﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameNode
{
    class Node
    {
        public int X { get; set; }
        public int Y { get; set; }
        protected Random rnd;

        public Node(int x, int y)
        {
            X = x;
            Y = y;
            rnd = new Random();
        }

        public void MoveUp()
        { Y--; }

        public void MoveDown()
        { Y++; }

        public void MoveLeft()
        { X--; }

        public void MoveRight()
        { X++; }

        public virtual void Move(Func<int, int, Position> positionStatus)
        {
            int randomMove = rnd.Next(1, 5);
            switch (randomMove)
            {
                case 1:
                    {
                        if (positionStatus(X, Y - 1) == Position.Free)
                            MoveUp();
                    }
                    break;

                case 2:
                    {
                        if (positionStatus(X, Y + 1) == Position.Free)
                            MoveDown();
                    }
                    break;

                case 3:
                    {
                        if (positionStatus(X - 1, Y) == Position.Free)
                            MoveLeft();
                    }
                    break;

                case 4:
                    {
                        if (positionStatus(X + 1, Y) == Position.Free)
                            MoveRight();
                    }
                    break;
            }
        }

        public bool IsOnPosition(int positionX, int positionY)
        {
            if(X == positionX && Y == positionY)
                return true;
            return false;
        }
    }

    class ConsoleNode : Node
    {
        public char NodeSymbol { get; private set; }
        public ConsoleColor NodeColor { get; private set; }

        public ConsoleNode(int x, int y, char nodeSymbol, ConsoleColor nodeColor)
            : base(x, y)
        {
            NodeSymbol = nodeSymbol;
            NodeColor = nodeColor;
        }
    }

    class ValeraNode:ConsoleNode
    {
        private int fieldHeight;
        private int fieldWidth;
        private bool touchedCeiling;
        private bool touchedRightWall;
        private bool fieldCompleted;
        private int counterH;
        private int counterW;
        private int currentWidth;
        public ValeraNode(int x, int y, char nodeSymbol, ConsoleColor nodeColor) 
            : base(x, y, nodeSymbol, nodeColor)
        {

        }

        public override void Move(Func<int, int, Position> positionStatus)
        {
            if ((positionStatus(X, Y - 1) != Position.Border)&&(touchedCeiling==false))
                MoveUp();
            else if((positionStatus(X, Y - 1) == Position.Border)&& (touchedRightWall == false)&&(positionStatus(X+1,Y)!=Position.Border))
                MoveRight();
            else if((positionStatus(X,Y-1)==Position.Border)&&(positionStatus(X+1,Y)==Position.Border))
            {
                touchedCeiling = true;
                touchedRightWall = true;
                MoveDown();
            }
            else if ((fieldCompleted == false) && (touchedCeiling == true)&& (touchedRightWall == false) && (positionStatus(X + 1, Y) == Position.Border))
            {
                
                MoveDown();
                fieldHeight++;
            }
            else if ((positionStatus(X - 1, Y) == Position.Border) && ((positionStatus(X, Y+1) == Position.Border)))
            {
                MoveUp();
                fieldHeight++;
                fieldCompleted = true;
                currentWidth = fieldWidth;
            }
            else if ((positionStatus(X - 1, Y) == Position.Border) && (fieldCompleted == true))
            {
                MoveUp();
            }
            //else if ((counterW < (fieldWidth - currentWidth))&&(fieldCompleted == true))
            //{
            //    {
            //        MoveRight();
            //        counterW++;
            //    }
            //}
            //else if ((counterW))
            //else if ((positionStatus(X, Y + 1) == Position.Free) && (counterW == (fieldWidth - 3))&&(fieldCompleted == true))
            //{
            //    MoveDown();
            //}
            }

    }
}
